﻿

using CommunityToolkit.Mvvm.Messaging;
using Company.Core.Enums;
using Company.Core.Interfaces;
using Company.Shell.Interfaces;
using Company.Shell.Models;
using Company.Shell.Services;
using Company.Shell.ViewModels;
using Microsoft.Extensions.DependencyInjection;
using System.Windows;

namespace Company.Shell.Views
{
    /// <summary>
    /// ShellView.xaml 的交互逻辑
    /// </summary>
    public partial class ShellView : HandyControl.Controls.Window
    {
        public ShellView(ICacheManager cacheManager,
                         IHardwareManager hardwareManager,
                         IConfigProvider configProvider,
                         IConfigManager configManager)
        {
            InitializeComponent();
            DataContext = App.Current.Services.GetService<ShellViewModel>();//将ShellViewModel注入到ShellView的DataContext

            //善后工作
            WeakReferenceMessenger.Default.Register<OnExitMessage>(this, (sender,e) =>
            {
                configManager.Write(ConfigType.System, configProvider.Config);//保存配置
                configManager.Write(ConfigType.Cache, cacheManager.Caches);//保存缓存
                configManager.Write(ConfigType.Alarm, configProvider.AlarmItems);//保存报警配置
                hardwareManager.Close();//硬件关闭
            });

        }
    }
}
